package com.zj.core.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.ImageView
import com.fileutils.FontUtils
import com.zj.core.R
import com.zj.core.bean.VoiceTemplateBean
import com.zj.core.tools.Constant
import com.zj.core.ui.TemplateChildUi
import com.zj.core.view.MeuseTextView
import org.jetbrains.anko.AnkoContext
import org.jetbrains.anko.find
import java.util.ArrayList
import com.fileutils.SPUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.zj.core.base.BaseApplication


class AdapterChilder(context: Context, detailList: ArrayList<VoiceTemplateBean.VoiceWordBean.DatEntity.RowsEntity>?, parentIndex: Int) : RecyclerView.Adapter<AdapterChilder.AdapterChilderHolder>() {
    private var mContext:Context?=context
    private var mParentIndex:Int=parentIndex
    private var mDetailList:ArrayList<VoiceTemplateBean.VoiceWordBean.DatEntity.RowsEntity>?=detailList
    private var mFirstLv:HashMap<String,String>?=null
    private var mGson:Gson?=Gson()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdapterChilderHolder {
        return AdapterChilderHolder(TemplateChildUi().createView(AnkoContext.create(parent.context, parent)))
    }

    override fun onBindViewHolder(holder: AdapterChilderHolder, position: Int) {
        SPUtils.get(BaseApplication.context,""+mParentIndex,"")?.let {
            mFirstLv=if(it.toString().isNotBlank()){
                mGson?.fromJson(it as String,HashMap<String,String>().javaClass)
            }else{
                hashMapOf()
            }
        }?:let{
            mFirstLv=hashMapOf()
        }


        with(holder){
            val mSecondItem=mFirstLv?.get(""+position)
            if(mSecondItem!=null&&mSecondItem!=""){
                this.checkBoxView?.isChecked=true
            }else{
                mFirstLv?.put(""+position,"")
                this.checkBoxView?.isChecked=false
            }

            if (position == 0) {
                this.recordItemLine?.visibility = View.GONE
            } else {
                this.recordItemLine?.visibility = View.VISIBLE
            }

            this.templateStrTv?.text = mDetailList?.get(position)?.wouldword
            this.pushImgView?.setOnClickListener {
                detailShowView(this, position)
            }

            this.checkBoxView?.setOnCheckedChangeListener{ compoundButton: CompoundButton, b: Boolean ->
                if(b){
                    mFirstLv?.set(""+position,mDetailList?.get(position)?.wouldword.toString())
                }else{
                    mFirstLv?.remove(""+position)
                }

                val myMap = HashMap<String, String>()
                for (entry in mFirstLv!!.entries) {
                    myMap[entry.key] = entry.value
                }
                SPUtils.put(BaseApplication.context,""+mParentIndex,mGson?.toJson(myMap))
            }

            this.templateStrTv?.setOnClickListener { detailShowView(this, position) }
            val fontWidth= FontUtils.meauseWidthForText(mContext,mDetailList?.get(position)?.wouldword)

            if (fontWidth> Constant.meuseTextViewWidth){
                this.templateStrTv?.post {
                    this.pushImgView?.visibility = View.VISIBLE
                }
            } else {
                this.templateStrTv?.post {
                    this.pushImgView?.visibility = View.INVISIBLE
                }
            }
        }
    }

    /**
     * 展开折叠
     *
     * @param holder
     * @param position
     */
    private fun detailShowView(holder: AdapterChilderHolder, position: Int) {
        with(holder){
            if (this.pushImgView?.tag == null || this.pushImgView?.tag == "0") {
                this.pushImgView?.setBackgroundResource(R.mipmap.simple_img)
                this.pushImgView?.tag = "1"

                this.templateStrTv?.maxLines = 99
                this.templateStrTv?.text = mDetailList?.get(position)?.wouldword
            } else {                                                         //折叠
                this.pushImgView?.setBackgroundResource(R.mipmap.detail_img)
                this.pushImgView?.tag = "0"

                this.templateStrTv?.maxLines = 1
                this.templateStrTv?.text = mDetailList?.get(position)?.wouldword
            }
        }
    }

    override fun getItemCount(): Int {
        return mDetailList!!.size
    }

    inner class AdapterChilderHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        var checkBoxView: CheckBox? =itemView.find(R.id.check_box_view)
        var templateStrTv: MeuseTextView? =itemView.find(R.id.template_str_tv)
        var pushImgView: ImageView? =itemView.find(R.id.push_img_view)
        var recordItemLine: View? =itemView.find(R.id.record_item_line)
    }
}